package com.etrans.web.gps.business.report.controller;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.domain.LngLat;
import com.etrans.web.gps.business.report.domain.ResultDocs;
import com.etrans.web.gps.business.report.service.AlarmHotAnalysisService;
import com.etrans.web.gps.business.report.service.AlarmTypeService;
import com.etrans.web.gps.business.system.domain.AlarmTypeVO;
import com.etrans.web.gps.util.map.GpsToGaoDe;

/**
 * 报警热区分析
 * 
 * @author fxw
 *
 */
@Controller
@RequestMapping("/report/*")
public class AlarmHotAnalysisController extends BaseController {
	private final static Logger log = LoggerFactory.getLogger("AlarmHotAnalysisController");
	@Autowired
	private AlarmHotAnalysisService alarmHotAnalysisService;
	@Autowired
	private AlarmTypeService alarmTypeService;

	@RequestMapping(value = "queryAlarmHotAnalysisInfo.html")
	public ModelAndView queryAlarmHotAnalysisInfo(HttpServletRequest request,
			HttpServletResponse response) throws ParseException {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/views/business/report/alarmHotAnalysis");
	
		return modelAndView;
	}
	
	@RequestMapping(value = "queryAlarmType.html")
	@ResponseBody
	public List<HashMap<String,Object>> queryAlarmType(){       
		return alarmTypeService.queryAlarmType();		
	}
	
	
	@RequestMapping(value = "queryAlarmHotAnalysisByTime.html")
	@ResponseBody
	public List<HashMap<String, Object>> queryAlarmHotAnalysisByTime(HttpServletRequest request,
			HttpServletResponse response,String startTime,String endTime,Integer alarmType) throws Exception {
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("start_Time", startTime);
		param.put("end_Time",endTime);
		param.put("alarmType", alarmType);
		ArrayList<HashMap<String, Object>> resultDataList = new ArrayList<HashMap<String, Object>>();
		double[] lnglat = new double[2];
		double lon = 0;
		double lat = 0;		
		// 查询_id关联的结果
		List<ResultDocs>queryList=alarmHotAnalysisService.queryAlarmHotAnalysisByTime(param);
		for (ResultDocs result_docs: queryList) {
				 LngLat[] coordinatesArrays = result_docs.getCoordinates();
				 if (coordinatesArrays.length>0) {
					 for (LngLat coordinate : coordinatesArrays) {
							lnglat = coordinate.getCoordinates();			
							lon = lnglat[0];
							lat = lnglat[1];
							HashMap<String, Object> item = new HashMap<String, Object>();
							double[] transform = GpsToGaoDe.transform(lon, lat);
							item.put("lng", transform[0]);
							item.put("lat", transform[1]);
							item.put("count", coordinatesArrays.length * 10);
							resultDataList.add(item);
						}
				}
		}		
		return resultDataList;		
	}

}
